Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@brightleaf/react-hooks
Advanced tools
Useful React Hooks
useAsync
- Hook to an async functionuseFetch
- Hook to use FetchuseGet
- Hook to make a get requestusePost
- Hook to make an HTTP PostuseClickOutside
- Hook to handle a click outside an elementuseGraphQL
- Hook to make a GraphQL requestuseKeypress
- Hook to KeypressuseNes
- Hook to connect to Hapijs NESuseWebSockets
- Hook to interact with WebSocketsuseLocalStorage
- Hook to local storageuseHover
- Hook for binding to hover of an elementuseEventListener
- Hook for binding to an hander to DOM eventuseFavicon
- Hook to set a faviconuseTitle
- Hook to manipulate the page titleuseScript
- Hook to add JavaScript to the pageuseStyles
- Hook to add CSS to the pageuseStyleSheet
- Hook to add a css file to the pageusePageVisibility
- Hook to use the page visibility apiuseOnlineStatus
- Hook to use the online status apiuseMediaQuery
- Hook to return if the media query is matchedimport React, { useEffect } from 'react'
import { useAsync } from '@brightleaf/react-hooks'
export default () => {
const { loading, error, data, execute } = useAsync(asyncFunction)
useEffect(() => {
execute()
}, [])
if (loading) {
return <div>Executing Async Function</div>
}
if (error) {
return <div>Error executing async function</div>
}
return <div>{data}</div>
}
import React, { useState } from 'react'
import { useFetch } from '@brightleaf/react-hooks'
export default () => {
const [id, setId] = useState(1)
const { data, error, loading } = useFetch(
`https://swapiql.herokuapp.com/api/characters/${id}`
)
if (error) {
return (
<div className="App">
<h1>Error Getting Data</h1>
</div>
)
}
if (loading) {
return (
<div className="App">
<h1>Loading Data</h1>
</div>
)
}
return (
<div className="App">
<h1>{data[0].name}</h1>
</div>
)
}
import React, { useState } from 'react'
import { useGraphQL } from '@brightleaf/react-hooks'
export default () => {
const [id, setId] = useState(1)
const { data, loading, error } = useGraphQL(
'https://swapiql.herokuapp.com/graphql',
`query Person($id: ID) {
person(id: $id) {
id,
name
}
}`,
{ id }
)
if (loading) {
return <div>Loading Data</div>
}
if (error) {
return <div>Error getting graphql data</div>
}
return <div>{data.person[0].name}</div>
}
import React from 'react'
import { useKeypress } from '@brightleaf/react-hooks'
export default () => {
const aKeyDown = useKeypress('a')
return (
<div className="App content">
<h2>Key Press?</h2>
{aKeyDown && (
<div>
Yes, the <b>{'"a"'}</b> key is pressed
</div>
)}
{!keyDown && <div>No the <b>{'"a"'}</b> key is not currently pressed</div>}
</div>
)
}
import React from 'react'
import { Button, Form, TextBox } from 'react-form-elements'
import { usePost } from '@brightleaf/react-hooks'
export default () => {
const { data, error, loading, postData } = usePost(
`https://kev-pi.herokuapp.com/api/echo`
)
if (loading) {
return <div>Loading Data</div>
}
if (error) {
return <div>Error getting graphql data</div>
}
return (
<Form
name="testForm"
onSubmit={data => {
postData(data)
}}
>
<TextBox label="My Label" name="myTextBox" />
<Button>Send</Button>
</Form>
)
}
import React, { useState } from 'react'
import { useGet } from '@brightleaf/react-hooks'
export default () => {
const [id, setId] = useState(1)
const { data, error, loading, getUrl } = useGet(
`https://swapiql.herokuapp.com/api/characters/${id}`
)
if (error) {
return (
<div className="App">
<h1>Error Getting Data</h1>
</div>
)
}
if (loading) {
return (
<div className="App">
<h1>Loading Data</h1>
</div>
)
}
return (
<div className="App">
<h1>{data[0].name}</h1>
<h2>Pick a number</h2>
<div className="select">
<div
onClick={e => {
getUrl('https://swapiql.herokuapp.com/api/characters/2')
}}
>
2
</div>
</div>
</div>
)
}
import React, { useState, useRef } from 'react'
import { useClickOutside } from '@brightleaf/react-hooks'
export default () => {
const [menu, setMenu] = useState(false)
const navMenu= useRef()
const hideDropDown = () => {
setMenu(false)
}
useClickOutside(navMenu, hideDropDowns, menu)
return (
<div className="App">
<nav ref={navMenu}>
<div className={`dropdown ${menu ? 'active' : ' '}`}>
<button
type="button"
className="dropdown-trigger"
onClick={e => {
setMenu(!menu)
}}
>
Menu
</button>
<div>
...// items
</div>
</div>
</nav>
... // rest of page
</div>
)
}
import React, { useEffect } from 'react'
import { useTitle } from '@brightleaf/react-hooks'
export default () => {
useTitle('Brightleaf JS Hooks')
return <div>The Page</div>
}
Client hook for connecting to an NES instance
import React from 'react'
import { useNes } from '@brightleaf/react-hooks'
export default () => {
const { message, error, connecting, connected } = useNes(
'wss://kev-pi.herokuapp.com'
)
if (error) {
return <div><h1>Error</h1></div>
}
const connectMessage = connecting ? <div>Connecting</div> : <div>Not Connecting</div>
const connectedMessage = connected ? <div>Connected</div> : <div>Not Connected</div>
const messages = message.map((m, index) => <div key={`key-${index}`}>{m}</div>)
return (
<div className="App">
<h1>Messages from Server</h1>
<div>{messages}</div>
<div>
<b>Status Messages</b>
{connectMessage}
{connectedMessage}
</div>
</div>
)
}
Use the client from the hook to create a real time chat room
import React, { useState} from 'react'
import { useNes } from '@brightleaf/react-hooks'
export default () => {
const { client } = useNes('wss://kev-pi.herokuapp.com')
const [messages, setMessages] = useState([{
body: 'welcome',
user: 'system'
}])
const addMessage = (msg) => {
messages.push(msg)
setMessages([...messages])
}
const handler = function(update, flags) {
addMessage(update)
}
// subscribe to a channel
client.subscribe('/rooms/general', handler);
const messageList = messages.map(({user, body}, index) => (<div key={`key-${index}`}><b>{user}:</b>{body}</div>))
return (
<div>
{messageList}
</div>
)
}
import React, { useRef } from 'react'
import { useLocalStorage } from '@brightleaf/react-hooks'
export default () => {
const [count, setCount] = useLocalStorage(0);
return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}
import React, { useRef } from 'react'
import { useHover } from '@brightleaf/react-hooks'
export default () => {
const elRef = useRef(null);
const hovered = useHover(elRef);
return (
<div>
<div className={ hovered ? 'animated' : 'static' }>
Hover Over Me
</div>
</div>
);
}
import React from 'react'
import { useStyles } from '@brightleaf/react-hooks'
export default () => {
useStyles(`
html,
body {
font-family: 'Open Sans';
}
div {
padding: 5px;
border: 1px solid #ccc;
}
`)
return (
<div>
<div>
Something
</div>
</div>
);
}
import React from 'react'
import { useStyleSheet } from '@brightleaf/react-hooks'
export default () => {
useStyleSheet(
'https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css'
)
useStyleSheet('https://fonts.googleapis.com/css?family=Open+Sans')
return (
<div>
...
</div>
);
}
import React from 'react'
import { useScript } from '@brightleaf/react-hooks'
export default () => {
useScript('/js/script.js')
return (
<div>
...
</div>
);
}
import React from 'react'
import { usePageVisibility } from '@brightleaf/react-hooks'
export default () => {
const { visible, hidden } = usePageVisibility()
return (
<div>
...
</div>
);
}
import React from 'react'
import { useFavicon } from '@brightleaf/react-hooks'
export default () => {
const { favicon, setFavicon } = useFavicon('brightleaf.png')
return (
<div>
<button onClick={e => {
e.preventDefault()
setFavicon('brightleaf-1.png')
}}>Change Favicon</button>
</div>
);
}
import React from 'react'
import { useOnlineStatus } from '@brightleaf/react-hooks'
export default () => {
const { offline, online } = useOnlineStatus()
return (
<div>
<h3>
{offline && 'Connection is offline'}
{online && 'Connection is online'}
</h3>
{offline && (
<article className="message is-danger">
<div className="message-body">Connection is offline </div>
</article>
)}
{online && (
<article className="message is-success">
<div className="message-body">Connection is online </div>
</article>
)}
</div>
)
}
import React , { useState } from 'react'
import { useEventListener } from '@brightleaf/react-hooks'
export default () => {
const [state, setState] = useState(window.scrollY)
const onScroll = e => {
setState(window.scrollY)
}
useEventListener('scroll', onScroll)
return (
<div className="App content">
<h2>Scroll the page</h2>
<span
style={{
position: 'fixed',
top: 10,
left: 10,
background: '#CCCCCC',
}}
>
{state} scroll position
</span>
<Home></Home>
</div>
)
}
import React , { useState } from 'react'
import { useMediaQuery } from '@brightleaf/react-hooks'
export default () => {
const { matches } = useMediaQuery('(min-width: 500px)')
return (
<div className="App content">
{matches && (
<article className="message is-warning">
<div className="message-body">The page is at least 500px</div>
</article>
)}
</div>
)
}
FAQs
Useful react hooks
We found that @brightleaf/react-hooks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.